package com.century.sjt.activity.Pay;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.century.sjt.R;
import com.century.sjt.activity.BaseActivity;
import com.century.sjt.util.Constant;
import com.century.sjt.util.TipUtil;
import com.century.sjt.widget.TitleBar.SjtTitleBar;

import java.util.HashMap;
import java.util.Map;


/**
 * 订单详情
 */
public class TransactionDataActivity extends BaseActivity{
    private SjtTitleBar titleBar;
    private RequestQueue mQueue;
    private String txNo;
    private String tradeType;
    private String payType;
    private String isShop;
    private String isMerchant;
    private boolean onShop=false;

    private WebView tvComment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sjt_transactiondata_activity);
        Intent intent = getIntent();
        txNo = intent.getStringExtra("txNo");
        mQueue = Volley.newRequestQueue(this);

        SharedPreferences mainSharedPreferences =getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
        isShop = mainSharedPreferences.getString("isShop", "");
        isMerchant = mainSharedPreferences.getString("isMerchant","");
        if (!"0".equals(isShop)||!"0".equals(isMerchant)){
            onShop=true;
        }
        initView();
        initEvent();
        if ("".equals(intent.getStringExtra("payType"))||null==intent.getStringExtra("payType")){
            payType= "0";
        }else {
            payType = intent.getStringExtra("payType");
        }

        if (null!= txNo ||!"".equals(txNo)){
            switch (payType){
                case "1"://商家交易记录
                    getTaransaction();
                    break;
                case "2"://商家待结算订单
                    getNoTaransaction();
                    break;
                case "3"://用户交易记录
                    getTaransaction();
                    break;
                case "4"://用户待结算订单
                    getNoTaransaction();
                    break;
                case "5"://聊天订单详情
                    getNoTaransaction();
                    break;

                default:

                    break;

            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    protected void initView() {
        titleBar = (SjtTitleBar) findViewById(R.id.title_bar);
        //       标题栏
        titleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        titleBar.setTitle(getResources().getString(R.string.tab_pay_picture));
        tvComment=(WebView) findViewById(R.id.news_detail_webview);


    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }


    /**
     *  获取订单详情
     */
    private void getTaransaction() {
        //显示Progress Dialog
//        TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), TransactionDataActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.TransDetail, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                String Html = response.toString();
                tvComment.loadDataWithBaseURL("", Html,"text/html","utf-8","");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), TransactionDataActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("txNo",txNo);
                return map;

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
//                Log.e("cookie", cookieValue);
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);

    }


    /**
     *  获取未结算订单
     */
    private void getNoTaransaction() {
        //显示Progress Dialog
//       TipUtil.openProgressDialog(Constant.APPLICATION_NAME, getResources().getString(R.string.error_please_waitting), TransactionDataActivity.this);
        StringRequest stringRequest = new StringRequest(Request.Method.POST, Constant.SettlingTransDetail, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
//                TipUtil.closeProgressDialog();
                String Html = response.toString();
                tvComment.loadDataWithBaseURL("", Html,"text/html","utf-8","");
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                TipUtil.showToast(getResources().getString(R.string.error_network), TransactionDataActivity.this, 1);
                TipUtil.closeProgressDialog();
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> map = new HashMap<String, String>();
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String userId = mainSharedPreferences.getString("userId", "");
                map.put("loginId", userId);
                map.put("orderId",txNo);
                return map;

            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                SharedPreferences mainSharedPreferences = getSharedPreferences(Constant.TAG, Activity.MODE_PRIVATE);
                String cookie = mainSharedPreferences.getString("cookie", "");
                String rememberMeValue = mainSharedPreferences.getString("rememberMe", "");
                HashMap localHashMap = new HashMap();
                String cookieValue = cookie + ";" + rememberMeValue;
//                Log.e("cookie", cookieValue);
                localHashMap.put("Cookie", cookieValue);
                return localHashMap;
            }
        };
        mQueue.add(stringRequest);

    }


}
